﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lib.Utility;
using Module.BusinessLogic;
using Module.EntitiesInfo;
using MyUtility;

namespace Web
{
    public partial class dangnhap : System.Web.UI.Page
    {
        private string url;
        protected string userName = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["url"] != null && Request.QueryString["url"] != "")
                {
                    url = Request.QueryString["url"];
                    if (HttpContext.Current.Request.Cookies["CookieUser"] != null)
                    {
                        userName = MemberBL.GetValue();
                    }


                }
                else
                {
                    url = "/xemlive.aspx";
                }
            }
            catch (Exception)
            {
                               
            }

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ClearText();
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
             userName= txtUserName.Text;
            
            try
            {

                if (txtUserName.Text.Length == 0)
                {

                    MessageBox.Show("Bạn chưa nhập tên đăng nhập?");
                    txtUserName.Focus();
                    return;
                }
                if (txtPassword.Text.Length == 0)
                {

                    MessageBox.Show("Bạn chưa nhập mật khẩu?");
                    txtPassword.Focus();
                    return;
                }
                MemberInfo info = new MemberInfo();
                info = MemberBL.GetInfo(3, txtUserName.Text.Trim(), "");              

                if (info != null && txtUserName.Text.Trim() == info.UserName.Trim() &&
                    MySecurity.Encrypt_MD5(txtPassword.Text.Trim()).ToLower() == info.Password.ToString().Trim())
                {
                    MemberBL.SaveCookie(info.UserName);
                    Response.Redirect(url);
                   
                }
                else
                {
                    MessageBox.Show("Tên đăng nhập hoặc mật khẩu không đúng?");
                    txtUserName.Focus();
                    return;
                }

            }
            catch (Exception ex)
            {                
            }

        }

        

        void ClearText()
        {
            txtUserName.Text = "";
            txtPassword.Text = "";            
        }
    }
}